﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;
using System.Threading;
namespace TS_XY_Train_Repaire.UserCon.Locomotive.Technology
{
    public partial class uc_Locomotive_MainBase_WorkFlow_WorkingDepRelation : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "";
        public string _LoadName = "";
        public uc_Locomotive_MainBase_WorkFlow_WorkingDepRelation()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_WorkFlow_WorkingDepRelation_Load(object sender, EventArgs e)
        {
            this.Text = _LoadName+"->>关联作业部门管理";
            _LoadOrgList();
            _LoadAuthGrid();
        }
        #region//--作业部门--//
        private void _LoadOrgList()
        {
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            ArrayList pList = new ArrayList();
            _ls = new LeverString_SysOrganization();
            _ls.f_id = "H";
            _ls.f_name = "组织结构";
            _ls.f_imgindex = 0;
            pList.Add(_ls);
            string strSQL = "select * from TS_S_T_P_RailwayBureau where f_del=0 order by f_id asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "B" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_rbid = read["f_id"].ToString();
                _ls.f_parent = "H";
                _ls.f_imgindex = 1;
                pList.Add(_ls);
            }
            read.Close();
            strSQL = "select * from TS_S_T_P_RailwayStations where f_upid in (select f_id from TS_S_T_P_RailwayBureau where f_del=0) and f_del=0 order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "S" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_rbid = read["f_upid"].ToString();
                _ls.f_rsid = read["f_id"].ToString();
                _ls.f_parent = "B" + read["f_upid"].ToString();
                _ls.f_imgindex = 2;
                pList.Add(_ls);
            }
            read.Close();
            strSQL = "select *,(select count(*) from TS_S_T_B_WorkMain_Dispatcher where f_type=" + _LoadID.Substring(1) + " and f_depid=TS_S_T_P_Department.f_id and f_del=0) as f_count from TS_S_T_P_Department where f_rsid in (select f_id from TS_S_T_P_RailwayStations where f_upid in (select f_id from TS_S_T_P_RailwayBureau where f_del=0) and f_del=0) and f_del=0 and f_deep=0 order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            string _strDepQuery = " and (";
            int i = 0;
            while (read.Read())
            {
                if (i == 0)
                {
                    _strDepQuery += "(f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                else
                {
                    _strDepQuery += " or (f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "D" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_parent = "S" + read["f_rsid"].ToString();
                if (read["f_count"].ToString()=="0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                _ls.f_imgindex = 3;
                pList.Add(_ls);
                i++;
            }
            _strDepQuery += ")";
            read.Close();
            strSQL = "select *,(select count(*) from TS_S_T_B_WorkMain_Dispatcher where f_type=" + _LoadID.Substring(1) + " and f_depid=TS_S_T_P_Department.f_id and f_del=0) as f_count from TS_S_T_P_Department where f_rsid in (select f_id from TS_S_T_P_RailwayStations where f_upid in (select f_id from TS_S_T_P_RailwayBureau where f_del=0) and f_del=0) " + _strDepQuery + " and f_deep>0 and f_del=0 order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "D" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                string _UPID = "";
                _UPID = read["f_level"].ToString();
                _UPID = _UPID.Substring(0, _UPID.Length - 1);
                _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                _ls.f_parent = "D" + _UPID;
                _ls.f_imgindex = 3;
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                pList.Add(_ls);
            }
            read.Close();
            conn.Close();
            conn.Dispose();

            tl_Main.DataSource = pList;
            tl_Main.ExpandAll();
        }
        #endregion
        private void _LoadAuthGrid()
        {
            string strSQL = "select f_id,convert(bit,0) as f_ck,row_number() over (order by f_id) as f_row,f_name  from TS_S_T_B_WorkMain_Dispatcher where f_type=" + _LoadID.Substring(1) + " and f_del=0";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.OK;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _FuncList = GetFunclIst(tl_Main.Nodes, "");
            TS_Win_Class.MySqlCom("update TS_S_T_B_WorkMain_Dispatcher set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_type=" + _LoadID.Substring(1)+" and f_del=0");
            if (_FuncList.Length > 0)
            {
                _FuncList = _FuncList.Substring(0, _FuncList.Length-1);
                //string[] _arry = _FuncList.Split(',');
                //for (int i = 0; i < _arry.Length; i++)
                //{
                //    if (_arry[i].Length > 0)
                //    {
                strSQL += "insert into TS_S_T_B_WorkMain_Dispatcher(f_type,f_name,f_depname,f_depid,f_workerid)  select " + _LoadID.Substring(1) + " as f_type,f_name,f_name as f_depname,f_id as f_depid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_Department where f_id in ("+ _FuncList + ")";
                //    }
                //}
                TS_Win_Class.MySqlCom(strSQL);
            }
            Thread.Sleep(50);
            _LoadAuthGrid();
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            try
            {
                string _str = "";
                for (int i = 0; i < gv_Main.DataRowCount; i++)
                {
                    if ((bool)gv_Main.GetRowCellValue(i, "f_ck"))
                    {
                        _str += gv_Main.GetRowCellValue(i, "f_id").ToString() + ",";
                    }
                }
                _str = _str.Substring(0, _str.Length - 1);
                TS_Win_Class.MySqlCom("update TS_S_T_B_WorkMain_Dispatcher set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where  f_id in(" + _str + ")");
                _LoadOrgList();
                _LoadAuthGrid();
            }
            catch
            {

            }
        }
        private string GetFunclIst(TreeListNodes _Nodes, string _str)
        {
            string _Str = _str;
            foreach (TreeListNode node in _Nodes)
            {
                if ((bool)node.GetValue("f_ck") && node.GetValue("f_id").ToString().Substring(0,1)=="D")
                {
                    _Str += node.GetValue("f_id").ToString().Substring(1) + ",";
                }
                if (node.HasChildren)
                {
                    _Str = GetFunclIst(node.Nodes, _Str);
                }
            }
            return _Str;
        }

        private void uc_Locomotive_MainBase_WorkFlow_WorkingDepRelation_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount>0)
            {
                bt_Del.Enabled = true;
                bt_Edit.Enabled = true;
                tb_Name.Text= gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_name").ToString();
            }
            else
            {
                bt_Del.Enabled = false;
                bt_Edit.Enabled = false;
                tb_Name.Text = "";
            }
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            if (tb_Name.Text.Trim().Length>0 && gv_Main.SelectedRowsCount>0)
            {
                bt_Edit.Enabled = true;
            }
            else
            {
                bt_Edit.Enabled = false;
            }
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            TS_Win_Class.MySqlCom("update TS_S_T_B_WorkMain_Dispatcher set f_name='"+tb_Name.Text.Trim()+"',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where  f_id="+ gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString());
            tb_Name.Text = "";
            _LoadAuthGrid();
        }
    }
}